package com.tianhui.consignortwofei.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.interf.IChartViewInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * @author fgs
 * @date 2021/2/23
 * @description
 **/
public class LineChartView extends LinearLayout {
    private LineChart lineChart;
    private TextView titleTextView;
    private int color;
    private List<String> lineLabelList = new ArrayList<>();
    private List<String> lineValueList = new ArrayList<>();

    public LineChartView(Context context) {
        this(context, null);
    }

    public LineChartView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LineChartView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.LineChartView);
        String title;
        try {
            title = typedArray.getString(R.styleable.LineChartView_LineChartViewTitle);
            color = typedArray.getColor(R.styleable.LineChartView_LineChartViewLineColor, getResources().getColor(R.color.colorBlueLight));
        } finally {
            typedArray.recycle();
        }
        View view = LayoutInflater.from(context).inflate(R.layout.layout_line_chart_view, null);
        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(layoutParams);
        addView(view);

        lineChart = view.findViewById(R.id.layout_line_chart_view_lineChartView);
        titleTextView = view.findViewById(R.id.layout_line_chart_view_titleTextView);

        initLineChartView();
        setLineChartTitle(title);
        setLineColor(color);
    }

    public void setLineChartTitle(String title) {
        titleTextView.setText(title);
    }

    public void setLineColor(int color) {
        this.color = color;
    }

    private void initLineChartView() {
        lineChart.setBackgroundColor(getResources().getColor(android.R.color.transparent));
        lineChart.getDescription().setEnabled(false);
        lineChart.setDrawGridBackground(false);
        lineChart.getLegend().setEnabled(false);
        lineChart.setTouchEnabled(true);
        lineChart.animateX(2000);


        XAxis xAxis = lineChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawLabels(true);
        xAxis.setDrawAxisLine(true);
        xAxis.setDrawGridLines(false);
        xAxis.setAxisMinimum(0f);
        xAxis.setGranularity(1f);
        xAxis.setGridColor(color);
//        xAxis.setTextColor(color);
//        xAxis.setAxisLineColor(color);
        xAxis.setLabelCount(7);
        xAxis.setLabelRotationAngle(-45);
        xAxis.setValueFormatter(new LineValueFormatter());

        YAxis yAxisLeft = lineChart.getAxisLeft();
        yAxisLeft.setAxisMinimum(0f);
        yAxisLeft.setDrawAxisLine(true);
        yAxisLeft.setDrawGridLines(false);
//        yAxisLeft.setTextColor(color);
//        yAxisLeft.setAxisLineColor(color);
        yAxisLeft.setDrawLabels(true);


        YAxis yAxisRight = lineChart.getAxisRight();
//        //解决右侧被遮挡
//        yAxisRight.setTextSize(5f);
//        //解决右侧被遮挡
//        yAxisRight.setTextColor(Color.TRANSPARENT);
//        //解决右侧被遮挡
//        yAxisRight.setAxisLineColor(Color.TRANSPARENT);
        yAxisRight.setEnabled(false);

    }

    private class LineValueFormatter extends ValueFormatter {
        @Override
        public String getAxisLabel(float value, AxisBase axis) {

//            String[] chartLabel = new String[]{"2020-02-22", "2020-02-22", "2020-02-22", "2020-02-22", "2020-02-22", "2020-02-22", "2020-02-22"};

            int index = (int) value;
            if (index < 0 || index >= lineLabelList.size()) {
                return "";
            } else {
                return lineLabelList.get(index);
            }
        }
    }

    public void setLineChartData(List<IChartViewInfo> infoList) {

        if (infoList == null) return;
        lineValueList.clear();
        lineLabelList.clear();
        int size = infoList.size();
        IChartViewInfo info;
        if (size > 0) {

            XAxis xAxis = lineChart.getXAxis();
            if (size > 15) {
                xAxis.setLabelCount(15);
            } else {
                xAxis.setLabelCount(size);
            }

            for (int i = 0; i < size; i++) {
                info = infoList.get(i);
                lineLabelList.add(info.getLabel());
                lineValueList.add(info.getValue());
            }

            ArrayList<ILineDataSet> dataSets = new ArrayList<>();

            ArrayList<Entry> values1 = new ArrayList<>();

            int length = lineValueList.size();
            if (length > 0) {
                for (int i = 0; i < length; i++) {
                    values1.add(new Entry(i, Float.valueOf(lineValueList.get(i))));
                }
                LineDataSet d1 = new LineDataSet(values1, "");
                d1.setDrawFilled(false);
                d1.setDrawCircleHole(false);
                d1.setDrawCircles(true);
                d1.setDrawValues(true);
                d1.setLineWidth(1.5f);
                d1.setMode(LineDataSet.Mode.HORIZONTAL_BEZIER);
                d1.setCircleRadius(2f);
                d1.setFillColor(color);
                d1.setValueTextColor(color);
                d1.setValueTextSize(10f);
                d1.setCircleColor(color);
                d1.setColor(color);
                d1.setValueFormatter(new ValueFormatter() {
                    @Override
                    public String getFormattedValue(float value) {
//                        int result = (int) value;
                        String result = value + "";
                        return result + "";
                    }
                });

                dataSets.add(d1);

                LineData data = new LineData(dataSets);
                lineChart.setData(data);

                lineChart.invalidate();
            }

        }

    }

}
